<?php

namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;

class VipLevelModel extends Model
{
	const STATUS_ACTIVE = 1;

	protected $connection = 'db_main';

	protected $table = 'wp_sdk_member_vip_level';

	public $timestamps = false;

    public static function info($level_id = 0)
    {
        return self::where(["status" => self::STATUS_ACTIVE, "level_id" => $level_id])->first();
    }

    /**
     * @desc 获取用户VIP权限
     * @param int $vipLevel
     * @param string $name
     * @return mixed
     */
    public static function getVipPowerByDb($vipLevel = 0, $name = '') {
        static $powers;
        if(!isset($powers[$vipLevel][$name]) || !$powers[$vipLevel][$name]) {
            $map['status']      = self::STATUS_ACTIVE;
            $map['level_id']    = $vipLevel;

            //$levelInfo = DB::table('sdk_member_vip_level_extra')->where($map)->first();
            $powers[$vipLevel][$name] = self::getLevelExtraInfoByKey($vipLevel, $name);
        }
        return $powers[$vipLevel][$name];
    }

    /**
     * 获取会员级别信息
     * @param int $levelId
     * @param string $key
     * @return bool
     */
    public static function getLevelExtraInfoByKey($levelId = 0, $key = '') {
        if(!$key) return false;

        $levelInfo = VipLevelExtraModel::info($levelId);
        if(!$levelInfo) return false;
        if($key == 'title') return $levelInfo->name;

        $levelType = $levelInfo['user_level_type'];
        $productInfo = ProductModel::info($key, $levelType);
        if(!$productInfo) return false;

        $config = $productInfo->config;
        $map['product_id']  = $productInfo->id;
        $data = VipLevelExtraModel::getValue('item_value', $map);
        if($config == 1) $data = ($data == 1) ? true : false;
        return $data;
    }
}

